package com.relife.core.easyui;

import com.relife.common.utils.StringUtil;
import com.relife.core.service.ProcessService;
import com.relife.core.spring.SpringContextUtil;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;



public class FlowListTag extends TagSupport {

	private static final long serialVersionUID = -4134622892594943541L;
	private static final ProcessService processService = (ProcessService) SpringContextUtil.getService("processService");
	private String url;
	private String pId;

	@Override
	public int doStartTag() throws JspTagException {
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			JspWriter out = this.pageContext.getOut();
			out.print(StringUtil.isEmpty(pId) ? getHtml() : getUiTable());
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	/**
	 * 
	 * @author WU
	 * @date 2016年5月27日 下午4:27:48
	 * @return
	 */
	private StringBuffer getHtml() {
		StringBuffer sb = new StringBuffer();
		sb.append("<table class=\"easyui-datagrid\" style=\"display: none\"");
		sb.append("data-options=\"singleSelect:true,rownumbers:true,border:false,fit:true,striped:true,");
		sb.append("url:'" + url + "'\">\n");
		sb.append("<thead>\n");
		sb.append("<tr>\n");
		sb.append("<th data-options=\"field:'pId'\">流程实例ID</th>\n");
		sb.append("<th data-options=\"field:'nodeName'\">节点名称</th>\n");
		sb.append("<th data-options=\"field:'startTime'\">开始时间</th>\n");
		sb.append("<th data-options=\"field:'endTime'\">结束时间</th>\n");
		sb.append("<th data-options=\"field:'processType'\">办理状态</th>\n");
		sb.append("<th data-options=\"field:'userName'\">办理人</th>\n");
		sb.append("<th data-options=\"field:'msg'\">办理意见</th>\n");
		sb.append("</tr>\n");
		sb.append("</thead>\n");
		sb.append("</table>\n");
		return sb;
	}

	/**
	 * 
	 * @author WU
	 * @date 2017年3月30日 上午10:01:20
	 * @return
	 */
	private StringBuffer getUiTable() {
		StringBuffer sb = new StringBuffer();
		sb.append("<table style='width:100%' class='uiTab'>\n");
		sb.append("<thead>\n");
		sb.append("<tr>\n");
		sb.append("<th width='14px'></th>\n");
		sb.append("<th>流程实例ID</th>\n");
		sb.append("<th>节点名称</th>\n");
		sb.append("<th>开始时间</th>\n");
		sb.append("<th>结束时间</th>\n");
		sb.append("<th>办理状态</th>\n");
		sb.append("<th>办理人</th>\n");
		sb.append("<th width='45%'>办理意见</th>\n");
		sb.append("</tr>\n");
		sb.append("</thead>\n");
		sb.append("<tbody>\n");
		List<Map<String, Object>> list = processService.flowList(pId);
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				sb.append("<tr>\n");
				sb.append("<th>" + (i + 1) + "</th>\n");
				sb.append("<td>" + list.get(i).get("pId") + "</td>\n");
				sb.append("<td>" + list.get(i).get("nodeName") + "</td>\n");
				sb.append("<td>" + list.get(i).get("startTime") + "</td>\n");
				sb.append("<td>" + (StringUtil.isNotEmpty(list.get(i).get("endTime")) ? list.get(i).get("endTime") : "") + "</td>\n");
				sb.append("<td>" + (StringUtil.isNotEmpty(list.get(i).get("processType")) ? list.get(i).get("processType") : "") + "</td>\n");
				sb.append("<td>" + (StringUtil.isNotEmpty(list.get(i).get("userName")) ? list.get(i).get("userName") : "") + "</td>\n");
				sb.append("<td>" + (StringUtil.isNotEmpty(list.get(i).get("msg")) ? list.get(i).get("msg") : "") + "</td>\n");
				sb.append("</tr>\n");
			}
		}
		sb.append("</tbody>\n");
		sb.append("</table>\n");
		return sb;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getpId() {
		return pId;
	}

	public void setpId(String pId) {
		this.pId = pId;
	}

}
